import torch
import torch.nn as nn

"""
全连接(FC)：线性层 nn.Linear() 每一个结点都跟后面的所有神经元相连
上一层的输出作为下一层的输入
前馈神经网络  FFN
多层感知机  MLP
"""

model = nn.Sequential(
    nn.Linear(1, 128),
    nn.Linear(128, 256),
    nn.Linear(256, 128),
    nn.Linear(128, 64),
    nn.Linear(64, 10)
)

x = torch.rand(4, 1)
y = model(x)
print(y.shape)


# ------------------------------------------------------------------
class MLP(nn.Module):
    def __init__(self, input_dim=1, output_dim=10):
        super().__init__()
        self.fc1 = nn.Linear(input_dim, 128)
        self.fc2 = nn.Linear(128, 256)
        self.fc3 = nn.Linear(256, 128)
        self.fc4 = nn.Linear(128, 64)
        self.fc5 = nn.Linear(64, output_dim)

    def forward(self, x):
        return self.fc5(self.fc4(self.fc3(self.fc2(self.fc1(x)))))


model2 = MLP()
x = torch.rand(4, 1)
y = model2(x)
print(y.shape)

# ----------------------------------------------------------------------
model3 = nn.ModuleList()
# ModuleList()是动态模块列表(支持 索引 / 迭代 )，自动注册子模块
input_sizes = [1, 128, 256, 128, 64, 10]
for i in range(len(input_sizes) - 1):
    model3.append(nn.Linear(input_sizes[i], input_sizes[i + 1]))
# 注意：要手动定义 forward() 方法
print(model3)
